﻿
var passregex = new RegExp("[0-9]")
var passregex2 = new RegExp("[a-zA-Z]")
var passregex3 = new RegExp(".{8,}")
//("^.*(?=.{8,})(?=.*\d)(?=.*[a-zA-Z]).*$");
var emailregex = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
var usernameregex = new RegExp("^([a-zA-Z0-9]){4,16}$");




//                                  BEGINNING OF RELATED FUNCTIONS
//------------------------------------------------------------------------------------------------------------

//MOVED LISTBOX_HIDE DIRECTLY INTO CONTENT PAGES

//------------------------------------------------------------------------------------------------------------
//                                    END OF RELATED FUNCTIONS

   

   $(window).scroll(function() {
        if($(window).scrollTop() > $('#locator').offset().top){
            $('#toc').css('top',$(window).scrollTop() - $('#locator').offset().top);
        } else {
            $('#toc').css('top','0');
        };
    });
   
  
    
  function changeAttr(myID,myAttr, newProp){
        $(myID).attr(myAttr,newProp);
    };

    function mlb(myBox) {
        $(myBox).css('border-color', 'gray');
        for (var i = 0; i < asts.length; i += 2) {
            //if ($(asts[i]) == '[object HTMLSelectElement]') {

                if (($(asts[i]).val() != 'Select a department') && ($(asts[i]).val() != 'Select a university') && ($(asts[i]).val() != '') && ($(asts[i + 1]).css('color') == 'rgb(255, 0, 0)')) {
                    $(asts[i + 1]).css('color', 'black');
                };
          //  } else {
              //  if (($(asts[i]).val() != '') && ($(asts[i + 1]).css('color') == 'rgb(255, 0, 0)')) {
               //     $(asts[i + 1]).css('color', 'black');
               // };
           // };
        };
    };
    
    $(function(){
        $('.searchboxclass').val('');
        $('.searchboxclass').focus();
        $('.searchboxclass').focus(function(){
            $('#mask_text').fadeOut('fast');
        });
        $('.searchboxclass').blur(function(){
            if($(this).val()==''){
                $('#mask_text').fadeIn('fast');
            };
        });
        $('.searchboxclass').keypress(function(event){
            if($('#mask_text').css('display')!='none'){
                $('#mask_text').fadeOut('fast');
            };
            var key1 = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            if(key1==13){
                alert('TODO: Create search function.');
            };
        });
        $('.searchboxclass').click(function(){
            $('#mask_text').fadeOut('fast');
        });
    });

    function validate_cna2() {
        var valid = true
        for (var i = 0; i < reqs.length; i++) {
            if ($(reqs[i]).val() == '') {
                valid = false
            };
        };
        if (valid == false) {

            return false
        } else {
            return true
        };

    };

    function vai(myBox) {
        if ($(myBox).val() == '' || $(myBox).val() == 'Select a department' || $(myBox).val() == 'Select a university') {
            $(myBox + '_aster').css('color', 'red');
        };
    };

    function regval(myBox, regtype) {
        if (regtype == 'pass') {
            var myInput = $(myBox).val()
            if (myInput.match(passregex) === null ||
                        myInput.match(passregex2) === null || myInput.match(passregex3) === null) {
                return false
            } else {
                return true
            };
        };
        if (regtype == 'email') {
            var myInput = $(myBox).val()
            if (myInput.match(emailregex) === null) {
                return false
            } else {
                return true
            };
        };
        if (regtype == 'username') {
            var myInput = $(myBox).val()
            if (myInput.match(usernameregex) === null) {
                return false
            } else {
                return true
            };
        };

    };

    
    function miscbutton_click(sr) {
        if (sr == '<%=cna2continuebuttonlink.clientid %>') {
            if ($('.cna2passwordboxclass').val() != ($('.cna2confirmpasswordboxclass').val())) {
                $('.cna2confirmpasswordboxclass').focus();
                $('.cna2confirmpasswordboxclass_aster').css('color', 'red');
                window.scrollTo(1, 1);
                $('#servererror').css('display', 'none');
                $('#emailerror').css('display', 'none');
                $('#passworderror').css('display', 'none');
                $('#usernameerror').css('display', 'none');
                $('#confirmerror').css('display', 'block');
                $('#emptyerror').css('display', 'none');
                $('.statuslabelclass').css('display', 'none');
                return false
            };
            if (validate_cna2() == false) {                         //If any required fields are blank, run the "vai" script
                vai('.cna2usernameboxclass');
                vai('.cna2emailboxclass');
                vai('.cna2passwordboxclass');
                vai('.cna2confirmpasswordboxclass');
                vai('.cna2_3');
                vai('.cna2_dropdown1');
                window.scrollTo(1, 1);
                $('#servererror').css('display', 'none');
                $('#emailerror').css('display', 'none');
                $('#passworderror').css('display', 'none');
                $('#usernameerror').css('display', 'none');
                $('#confirmerror').css('display', 'none');
                $('#emptyerror').css('display', 'block');
                $('.statuslabelclass').css('display', 'none');
                return false
            };
            if (regval('.cna2passwordboxclass', 'pass') == false) {
                $('input').blur();
                $('.cna2passwordboxclass').focus();
                $('.cna2passwordboxclass_aster').css('color', 'red');
                window.scrollTo(1, 1);
                $('#servererror').css('display', 'none');
                $('#emailerror').css('display', 'none');
                $('#passworderror').css('display', 'block');
                $('#usernameerror').css('display', 'none');
                $('#confirmerror').css('display', 'none');
                $('#emptyerror').css('display', 'none');
                $('.statuslabelclass').css('display', 'none');
                return false
            };
            if (regval('.cna2emailboxclass', 'email') == false) {
                $('input').blur();
                $('.cna2emailboxclass').focus();
                $('.cna2emailboxclass_aster').css('color', 'red');
                window.scrollTo(1, 1);
                $('#servererror').css('display', 'none');
                $('#emailerror').css('display', 'block');
                $('#passworderror').css('display', 'none');
                $('#usernameerror').css('display', 'none');
                $('#confirmerror').css('display', 'none');
                $('#emptyerror').css('display', 'none');
                $('.statuslabelclass').css('display', 'none');
                return false
            };
            if (regval('.cna2usernameboxclass', 'username') == false) {
                $('input').blur();
                $('.cna2usernameboxclass').focus();
                $('.cna2usernameboxclass_aster').css('color', 'red');
                window.scrollTo(1, 1);
                $('#servererror').css('display', 'none');
                $('#emailerror').css('display', 'none');
                $('#passworderror').css('display', 'none');
                $('#usernameerror').css('display', 'block');
                $('#confirmerror').css('display', 'none');
                $('#emptyerror').css('display', 'none');
                $('.statuslabelclass').css('display', 'none');
                return false
            };
            if (($('.cna2secondaryemailboxclass').val() != '') && regval('.cna2secondaryemailboxclass', 'email') == false) {
                $('input').blur();
                $('.cna2secondaryemailboxclass').focus();
                $('.cna2secondaryemailboxclass_aster').css('color', 'red');
                window.scrollTo(1, 1);
                $('#servererror').css('display', 'none');
                $('#emailerror').css('display', 'block');
                $('#passworderror').css('display', 'none');
                $('#usernameerror').css('display', 'none');
                $('#confirmerror').css('display', 'none');
                $('#emptyerror').css('display', 'none');
                $('.statuslabelclass').css('display', 'none');
                return false
            };
            //if($('#secondaryemailtd:first').val()=='')
            return true
        };
    };

   

    function miscbutton_link(ar) {
        if (miscbutton_click('<%=cna2continuebuttonlink.clientid %>') == true) {
            __doPostBack('ctl00$Body$cna2continuebuttonlink', '');
        };
    };
    

    //----------------------------------CREATE NEW ACCOUNT 2 FUNCTIONS AND SCRIPTS-------------------------

    //---------------CREATE ARRAY OF REQUIRED INPUT BOXES FOR THIS PAGE-----------
    var asts = ['.cna2usernameboxclass', '.cna2usernameboxclass_aster',
               '.cna2passwordboxclass', '.cna2passwordboxclass_aster',
               '.cna2confirmpasswordboxclass', '.cna2confirmpasswordboxclass_aster',
               '.cna2_dropdown1', '.cna2_dropdown1_aster',
               '.cna2emailboxclass', '.cna2emailboxclass_aster']

    var reqs = ['.cna2usernameboxclass',
               '.cna2passwordboxclass',
               '.cna2confirmpasswordboxclass',
               '.cna2_dropdown1',
               '.cna2emailboxclass'];


    // $(function(){
    //   $('#cna2continuebutton').click(miscbutton_click('ctl00$MainContent$cna2continuebuttonlink'));
    //}); ------This script causes the page to be refreshed continuously.

    $(function() {
        $(document).keypress(function(myEvent) {
            var key = myEvent.keyCode ? myEvent.keyCode : myEvent.which ? myEvent.which : myEvent.charCode;
            if (((document.activeElement == '[object HTMLInputElement]') || (document.activeElement == '[object HTMLSelectElement]')) && key == 13) {
                // if (miscbutton_click('ctl00$MainContent$cna2continuebuttonlink') == true) {
                //     __doPostBack('ctl00$MainContent$cna2continuebuttonlink', '');
                // };
                if (miscbutton_click('<%=cna2continuebuttonlink.clientid %>') == true) {
                    __doPostBack('ctl00$Body$cna2continuebuttonlink', '');
                };
            };
        });
    });

    // handle selected input
    $(function() {
    var input = $('#cna2div input')
    var select = $('#cna2div select')
        input.corner('2px');
        select.corner('2px');
        input.focus(function() {
            $(this).css('border', 'solid 2px #C7262E');
            $(this).css('margin', '0 0 0 2px');
        });
        input.blur(function() {
            $(this).css('border', 'solid 1px gray');
            $(this).css('margin', '1px 1px 1px 3px');
            mlb(this);
        });
        select.focus(function() {
            $(this).css('border', 'solid 2px #C7262E');
        });
        select.blur(function() {
        $(this).css('border', 'solid 1px gray');
        mlb(this);
        });
    });


    function changeSrc(myid, myimg) {
        $(myid).attr('src', myimg);
    };





    //-----------------------------------------------------------------------------------------------------------------------------------------
    //                                      CLICK EVENTS

    $(document).click(function(e) {         //Controls the floating LOGIN BOX and DROP DOWN

        var main_loaddiv = document.getElementById('main_loaddiv')
        var myTarget = e.target
        var username_input = document.getElementById('<%=username_input.clientid %>');
        var password_input = document.getElementById('<%=password_input.clientid %>');
        var dep_ddb_container = document.getElementById('dep_ddb_container');
        var dep_ddb = document.getElementById('<%=dep_ddb.clientid %>');
        var dep_img = document.getElementById('dep_img');
        var filterButton = document.getElementById('<%=filterButton.clientid %>');
        if ((isDescendant(main_loaddiv, e.target) == false) &&
            (myTarget != main_loaddiv) && (login_display == true) &&
            !($(username_input).is(':focus') || $(password_input).is(':focus'))) {
            $('#main_dimmer').stop(0, 0).fadeTo(250, 1, function() { login_display = false });
            $(main_loaddiv).hide();
        } else {
            if (myTarget != dep_ddb_container && myTarget.parentNode != dep_ddb && myTarget != dep_img && $(dep_ddb).hasClass('dep_ddb_class_hidden') == false) {
                $(dep_ddb).toggleClass('dep_ddb_class_hidden', true);
                listbox_hide();

            };
        };


    });


    //          FUNCTIONS CONTROLLING THE DROP DOWN BOXES ARE PLACED DIRECTLY ON CONTENT PAGES


    $(function() { //           CONTROLS THE EXPANDING ITEM BOXES IN THE RESULTS PANEL

        $('.resultContainer_class').click(function(e) {
            var myClass = e.target.getAttribute('Class')
            if (myClass != 'star_div' && e.target.parentNode.getAttribute('Class') != 'star_div' &&
                myClass != 'postDetail') {
                $(this).toggleClass('resultContainerExpanded_class');
            };
        });
        $('.resultContainerExpanded_class').click(function(e) {
            var myClass = e.target.getAttribute('Class')
            if (myClass != 'star_div' && e.target.parentNode.getAttribute('Class') != 'star_div' &&
                myClass != 'postDetail') {
                $(this).toggleClass('resultContainer_class');
            };
        });
    });


    //------------------------------------------------------------------------------------------------------------
    //                                       END OF CLICK EVENTS
